<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>课程列表</title>
    <link rel="stylesheet/less" href="../../../static/css/index.less"/>
    <link rel="stylesheet" href="../../../static/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../../static/css/jquery.pagination.css"/>
    <script src="../../../static/js/jquery-1.12.3.js"></script>
    <script src="../../../static/js/less.min.js"></script>
    <script src="../../../static/layui/layui.js"></script>
    <script src="../../../static/js/ajaxfileupload.js"></script>
    <script src="../../../static/layer/layer.js"></script>
</head>
<style>
    .table_td_img {
        width: 50px;
        height: 50px;
        padding: 3px
    }
</style>
<body class="layui-layout-body">

{% include "admin/top.html" %}
{% include "admin/left.html" %}

<div id="box" class="fl">
    <div class="content">
        <h3><a>课程列表</a></h3>
        <div class="search">
                <input type="text" name="search" class="ipt" id="cname" placeholder="课程名称" value="">
                <select id="uid">
                    <option value="">选择教师</option>
                    {% if tc %}
                    {% for d in tc %}
                    <option value="{{ d.uid }}">{{ d.nickName }}</option>
                    {% endfor %}
                    {% endif %}
                </select>
                <select id="sid">
                    <option value="">选择主题</option>
                    {% if zt %}
                    {% for d in zt %}
                    <option value="{{ d.sid }}">{{ d.typeName }}</option>
                    {% endfor %}
                    {% endif %}
                </select>
                <select id="tid">
                    <option value="">选择分类</option>
                    {% if td %}
                    {% for d in td %}
                    <option value="{{ d.tid }}">{{ d.courseName }}</option>
                    {% endfor %}
                    {% endif %}
                </select>
                <select name="status" id="status">
                    <option value="">全部</option>
                    <option value="1">通过</option>
                    <option value="0">禁用</option>
                </select>
                <button onclick="get_data(1)" class="s_button">搜索</button>
        </div>
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <thead>
            <tr>
                <th>课程ID</th>
                <th>课程分类</th>
                <th>课程名称</th>
                <th>封面图</th>
                <th>讲师昵称</th>
                <th>创建时间</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
            </thead>

            <tbody id="result">
            <tr>
                {% if res %}
                    {% for d in res %}
                        <tr>
                            <td></td>
                            <td></td>
                            <td><a href=""></a></td>
                            <td><img src="" alt="" class="table_td_img"></td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td><a class="course_commit" name="edit">编辑</a><a class="course_commit" name="delete">删除</a>
                                <a class="course_commit" name="lock">锁定</a><a class="course_commit" name="unlock">激活</a></td>
                        </tr>
                    {% endfor %}
                    <tr>
                        <td colspan="6">暂无数据</td>
                    </tr>

                {% endif %}

            </tbody>
        </table>
            <div class="ui-pagination-container">

            </div>
    </div>
</div>
<input type="hidden" id="p" value="1">
</body>
</html>

<script>
    $('#sid').change(function(){
        var sid = $('#sid').val();
        $.ajax({
            url:"/admin/get_types_post",
            type:"post",
            data:{
                'sid':sid
            },
            dataType:"json",
            success:function(msg){
                var q = '<option value="">选择分类</option>';
                if (msg.status == 1){
                    for (var i = 0; i < msg.res.length; i++) {
                       q += '<option value="' + msg.res[i].tid + '">' + msg.res[i].courseName + '</option>';
                    }
                }
                $('#tid').html(q);
            }
        });
    });
    get_data(1);
    function get_data(p){
        var cname = $('#cname').val();
        var status = $('#status').val();
        var uid = $('#uid').val();
        var sid = $('#sid').val();
        var tid = $('#tid').val();
        $.ajax({
            url:"/admin/course_list_post",
            type:"post",
            data:{
                'cname':cname,
                'status':status,
                'p':p,
                'uid':uid,
                'sid':sid,
                'tid':tid
            },
            dataType:"json",
            success:function(msg){
                var q = '';
                if (msg.status == 1){
                    for (var i = 0; i < msg.res.length; i++){
                        q += '<tr>';
                        q += '<td>' + msg.res[i].cid + '</td>';
                        q += '<td>' + msg.res[i].tname + '</td>';
                        q += '<td>' + msg.res[i].courseName + '</td>';
                        q += '<td><img style="width: 60px; height: 35px;" src="' + msg.res[i].cover +'"></td>';
                        q += '<td>' + msg.res[i].nickName + '</td>';
                        q += '<td>' + msg.res[i].addTime + '</td>';
                        if (msg.res[i].status == 0){
                            q += '<td>禁用</td>';
                        }else {
                            q += '<td>激活</td>';
                        }
                        q += '<td><a class="course_commit" onclick="cilck_btn(' + msg.res[i].cid + ', 0)" name="edit">编辑</a>';
                        if (msg.res[i].status == 0){
                            q += '<a class="course_commit" onclick="cilck_btn(' + msg.res[i].cid + ', 1)" name="unlock">激活</a>';
                        }else {
                            q += '<a class="course_commit" onclick="cilck_btn(' + msg.res[i].cid + ', 2)" name="lock">锁定</a>';
                        }
                        q += ' <a class="course_commit" onclick="cilck_btn(' + msg.res[i].cid + ', 3)" name="delete">删除</a>';
                        q += '</td></tr>';
                    }

                }else {
                    q += '<td colspan="6">暂无数据</td>';
                }
                $('#result').html(q);
                $('.ui-pagination-container').html(msg.page);
                $('#p').val(msg.p);
            }
        })
    }
    function cilck_btn(uid, pid){
        if (pid == 0){
            location.href = '/admin/course_detail.html?cid=' + uid;
            return false;
        }else if(pid == 1){
            var content = "激活课程";
        }else if(pid == 2){
            var content = '禁用课程';
        }else if(pid == 3){
            var content = '删除课程';
        }else {
            return false;
        }
        layer.open({
                content: content,
                btn: ['确认', '取消'],
                yes: function () {
                    $.ajax({
                        url: "/admin/update_course_post",
                        type: "POST",
                        data: {
                            cid: uid,
                            pid: pid
                        },
                        success: function (data) {
                            if (data.status == 1) {
                                layer.msg("操作成功", {time: 2000}, function () {
                                    get_data($('#p').val());
                                })
                            } else {
                                layer.msg("操作失败！")
                            }
                        }
                    })
                },
                btn2: function (index, layero) {

                }
                ,
                cancel: function () {
                    //右上角关闭回调

                }
            });
    }

    $(".course_commit").click(function () {
        var cType = $(this).attr("name");
        var cId = $(this).parent().parent().children().html();
        console.log(cType);
        if (cType == "edit") {
            location.href = "/admin/course/edit.html?courseId=" + cId
        } else if (cType == "lock" || cType == "unlock" || cType == "delete") {
            var content = "";
            if (cType == "lock") {
                content = "你确定要锁定吗？"
            }
            else if (cType == "unlock") {
                content = "你确定要激活吗？"
            }
            else if (cType == "delete") {
                content = "你确定要删除吗？"
            }
            else {
                return
            }
            layer.open({
                content: content,
                btn: ['确认', '取消'],
                yes: function (index, layero) {
                    $.ajax({
                        url: "/admin/course/statusHandel",
                        type: "POST",
                        data: {
                            courseId: cId,
                            cType: cType
                        },
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg("操作成功", {time: 2000}, function () {
                                    location.reload()
                                })
                            } else {
                                layer.msg("操作失败！")
                            }
                        }
                    })
                },
                btn2: function (index, layero) {

                }
                ,
                cancel: function () {
                    //右上角关闭回调

                }
            });
        }
    });


</script>


